package chatserver.tools;


import java.util.Vector;

public class QSort {

    public QSort() {
    }

    private void QuickSort(Vector vector, int i, int j) {
        int k = i;
        int l = j;
        if (j > i) {
            String s = (String) vector.elementAt((i + j) / 2);
            while (k <= l) {
                while (k < j && ((String) vector.elementAt(k)).compareTo(s) < 0) {
                    k++;
                }
                for (; i < l && ((String) vector.elementAt(l)).compareTo(s) > 0; l--) {}
                if (k <= l) {
                    swap(vector, k, l);
                    k++;
                    l--;
                }
            }
            if (i < l) {
                QuickSort(vector, i, l);
            }
            if (k < j) {
                QuickSort(vector, k, j);
            }
        }
    }

    private void swap(Vector vector, int i, int j) {
        String s = (String) vector.elementAt(i);
        vector.setElementAt((String) vector.elementAt(j), i);
        vector.setElementAt(s, j);
    }

    public void sort(Vector vector) {
        QuickSort(vector, 0, vector.size() - 1);
    }
}
